Ext.ns('journal.home');

journal.home.EntryEditorHandler = function() {
    // do NOT access DOM from here; elements don't exist yet
    // private variables
    // private functions

    var logger = journal.LoggerFactory.getLogger('journal.home.EntryEditorHandler');

    // public space
    return {
        /**
         * @private
         */
        dirtyFlagToggler : function(tabpanel) {
            //Ext.getCmp('codeForm_' + tabpanel.getActiveTab().key);
            var codeForm = tabpanel.getActiveTab().get(0);
            //var formTitle = codeForm.getForm().findField('title').getValue();
            var formTabIndex = codeForm.getForm().findField('tabindex').getValue();

            if (!tabpanel.tabFlag[formTabIndex]) {
                tabpanel.tabFlag[formTabIndex] = true;
                var actTab = tabpanel.getActiveTab();
                actTab.setTitle('*' + actTab.title);

                Ext.getCmp(journal.Constant.Button.SAVE).setDisabled(false);
                var fileMenu = Ext.getCmp(journal.Constant.Menu.FILE);
                var fileSaveMenu = fileMenu.menu.items.get(journal.Constant.Menu.SAVE);
                fileSaveMenu.setDisabled(false);
            }
        },

        /**
         * Handles key events for textarea or html editor or both.
         * 
         * @param e - key event object
         * @param t - text form
         * @param scope -
         */
        editorKeyHandler : function(e, t, tabpanel, undoCount) {
            if (tabpanel == null) {
                return undoCount;
            }

            var htmleditor = tabpanel.activeTab.get(0).get(4);

            var key = String.fromCharCode(e.getKey()).toUpperCase().charCodeAt(0);
            //logger.debug(e.type + ':' + e.getKey() + ':' + key);

            if (e.button != -1 && (key >= e.A && key <= e.Z && !e.ctrlKey) || e.getKey() == e.BACKSPACE
                    || e.getKey() == e.DELETE || e.getKey() == e.ENTER
                    || (e.getKey() >= e.ZERO && e.getKey() <= e.NINE) || e.getKey() == e.SPACE || e.getKey() == e.TAB
                    || (e.ctrlKey && e.isSpecialKey() && (key == e.V || key == e.X)) || (e.ctrlKey && e.getKey() == 98)) {

                if (e.keyCode == e.TAB) {
                    if (e.shiftKey) {
                        var cancel = false; // do not process when not all
                        if (!cancel) {
                            undoCount++;
                        }
                    } else {// EO Shift Tab
                        htmleditor.insertAtCursor('&nbsp;&nbsp;&nbsp;&nbsp;');
                        undoCount++;
                    }// EO Tab
                    e.stopEvent();
                } else {// end of Tab Processing
                    undoCount++;
                }
                this.dirtyFlagToggler(tabpanel);
            } else if (e.ctrlKey && (e.getKey() == 122 || e.getKey() == e.Z)) {// Ctrl + Z
                if (undoCount == 0) {
                    e.stopEvent();
                } else {
                    undoCount--;
                }
            } else if (e.ctrlKey && e.getKey() == e.B) {
                e.stopEvent();
            } else if (e.isSpecialKey() && e.getKey() == e.ESC) {
                t.selectionStart = 0;
                t.selectionEnd = 0;
            }
            return undoCount;
        }
    };
}(); // end of app

// end of file

